package com.thinvent.nj.ais.service.impl;

import com.thinvent.nj.common.page.Page;
import com.thinvent.nj.common.page.QueryParameter;
import com.thinvent.nj.mybatis.service.impl.BaseCURDServiceImpl;
import com.thinvent.nj.ais.entity.Resource;
import com.thinvent.nj.ais.mapper.ResourceMapper;
import com.thinvent.nj.ais.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 资源Service
 * @author liupj
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class ResourceServiceImpl extends BaseCURDServiceImpl<Resource, String> implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Resource> findByRoleId(String roleId) {
        return resourceMapper.findByRoleId(roleId);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Resource> findAll() {
        List<Resource> resourceList = resourceMapper.findAll();
        return resourceList;
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public Resource get(String s) {
        return super.get(s);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys",allEntries = true)
    @Override
    public void delete(String s) {
        super.delete(s);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys",allEntries = true)
    @Override
    public void update(Resource resource) {
        super.update(resource);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys",allEntries = true)
    @Override
    public void insert(Resource resource) {
        super.insert(resource);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Resource> findList(Map<String, Object> params) {
        return super.findList(params);
    }

    @Cacheable(cacheNames = "sys", key = "'ResourceServiceImpl.findPage:' + #queryParameter.pageNo + '_' + #queryParameter.pageSize + '_' + #params")
    @Override
    public Page<Resource> findPage(QueryParameter queryParameter, Map<String, Object> params) {
        return super.findPage(queryParameter, params);
    }
}
